package com.example.activemq2;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;

import javax.jms.*;

public class Cos {
    @Test
    //模拟消费者
    public void xiaofeizhe() throws JMSException {
        //创建工厂 JMS用它创建连接  获取mq的连接
        ConnectionFactory connectionFactory= new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
        //创建连接
        Connection connection= connectionFactory.createConnection();
        //启动连接
        connection.start();
        //创建会话工厂
        Session session= connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
        //创建队列
        Destination destination= session.createQueue("com.example_one");
        //生产消息
        MessageConsumer consumer= session.createConsumer(destination);
        while (true){
            //监听消息
            TextMessage textMessage= (TextMessage) consumer.receive();
            if(textMessage!=null){
                String Text=  textMessage.getText();
                System.out.println("消费者消费完毕");
                //例二 必须 手动签收
                //textMessage.acknowledge();
            }else {
                break;
            }
        }

    }

}
